from bs4 import BeautifulSoup #
import time #时间操作
import os

#爬取并输出学生信息
from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException, WebDriverException


class Constants:
    STUNAME = 'stuName'
    STUNO = 'stuNo'
    COMPANYNAME = 'companyName'
    POSTNAME = 'postName'
    SALARY = 'salary'
    TEACHER = 'teacher'
    COMPANYADDR = 'companyAddr'
class ReptleStuMsg:
    def start(self,driver,codeUrl,target):
        self.basePath = target.basePath
        print("开始爬取")
        #点击个人信息
        flag = 0
        while flag<2:
            try:
                flag+=1
                print("循环",flag)
                time.sleep(2)
                elem = driver.find_element_by_class_name("border-radius-all")
                elem.click()
                flag=3 #跳出循环
            except ElementNotVisibleException as e:
                target.setCode(driver,codeUrl)
                continue
        #跳出这一次
        if flag==2:
            return
        time.sleep(1)

        html =BeautifulSoup(driver.page_source,'lxml')
        #保存学生信息
        stuMsg = self.saveToLocal(html)
        #跳转顶岗实习页面
        elem = driver.find_element_by_xpath(".//*[@code='dinggang']")
        elem.click()
        time.sleep(1)
        # print(driver.page_source)
        #关闭提示框
        elem = driver.find_element_by_xpath(".//*[@class='btn btn-primary']")
        try:
            elem.click()
        except ElementNotVisibleException as e:
            pass
        time.sleep(1)
        elem = driver.find_element_by_xpath(".//*[@code='dinggang.plan']")
        elem.click()
        time.sleep(1)
        elem = driver.find_element_by_xpath(".//*[@code='dinggang.plan.arra-student']")
        elem.click()
        time.sleep(1)
        #查看学生实习信息
        try:
            elem = driver.find_element_by_xpath(".//*[@ui-type='view']")
            elem.click()
        except NoSuchElementException as e:
            #没有实习信息
            return
        except WebDriverException as e:
            #有实习信息，a标签不能点击
            return
        time.sleep(1)

        #保存学生实习信息
        practiceMsg = self.saveCompanyMsg(driver)
        self.saveFile(stuMsg+practiceMsg)
    def saveToLocal(self,html):
        #学生姓名
        stuName = html.find('a','border-radius-all').find('span').text
        #学号
        stuNo = html.find('div',id='loginName').text.strip()
        #学生信息其他信息
        #信息列表 0 = 班级 1=手机号 2=手机短号,电子邮箱 ，qq号，微信号。
        msglist = html.find('table','edit-table').find_all('td')
        # print(msglist)
        content = Constants.STUNAME+":"+stuName+"\t" + Constants.STUNO+":"+stuNo+"\t"
        for i,item in enumerate(msglist) :
            if item.text:
                typeName = None
                if i==0:
                    typeName = "stuClass"
                if i==1: #手机号
                    typeName = "mobNo"
                # if i==2: #手机短号
                #    typeName = "mobNo"
                #      = item.text
                if i==3: #电子邮箱
                    typeName = "email"
                if i==4: #qq号
                    typeName = "qqNo"
                if i==5: #微信号
                    typeName = "wxNo"
                if i!=2:
                    content += typeName+":"+ item.text+"\t"
        return content
    def saveCompanyMsg(self,driver):
        time.sleep(2)
        html =BeautifulSoup(driver.page_source,'lxml')
        # 就业信息
        trs = html.find_all('table',class_="edit-table col4 view")[1].find_all("tr")
        for i,item in enumerate(trs):
            tds = item.find_all('td')
            if i==0:
                companyName = tds[0].text  #公司名称
                postName = tds[1].text  #岗位名称
            if i==1:
                salary = tds[1].text  #工资
            if i==2:
                teacher = tds[0].text  #校外老师

        #公司地址
        companyAddr = html.find_all('div',class_="field-set")[2]\
                        .find_all("span")[1].text

        content = Constants.COMPANYNAME + ":"+ companyName+"\t"\
                  + Constants.POSTNAME + ":"+ postName+"\t"\
                  + Constants.SALARY + ":"+ salary+"\t" \
                  + Constants.TEACHER + ":"+ teacher+"\t" \
                  + Constants.COMPANYADDR + ":"+ companyAddr+"\t"\
                  + "\r\n"
        return content;
    def saveFile(self,content):
        if not os.path.exists(self.basePath):
            print("——创建文件夹——")
            os.mkdir(self.basePath)
        else:
            print("——文件夹已经存在，不需要创建——")
        f = open(self.basePath+"/stu.txt",'ab')
        f.write(content.encode())
        print('保存成功！')
        f.close()
